
#pragma once

#include "ConfigChecker.h"
#include <map>

// 计数器
class Counter : CriticalObject
{
public:
    Counter(){  m_nRef = 0; }
    ~Counter(){  }

    void Reset()
    {
        SMART_LOCK_THIS();
        m_nRef = 0;
    }

    void Set(int ref)
    {
        SMART_LOCK_THIS();
        m_nRef = ref;
    }

    void IncRef()
    {
        SMART_LOCK_THIS();
        m_nRef ++;
    }

    void DecRef()
    {
        SMART_LOCK_THIS();
        m_nRef --;
    }

    void Wait0()
    {
        while(1)
        {
            Lock();
            if (m_nRef == 0)
            {
                Unlock();
                return;
            }
            Unlock();

            usleep(100);
        }
    }

public:
    int m_nRef;
};

//////////////////////////////////
void  Async_SelectPlayerName(CMsgTyped* pmsg,  sql::Statement* pConn);
void  Async_SelectFamilyName(CMsgTyped* pmsg,  sql::Statement* pConn);

void  Async_RenamePlayerNameManage(CMsgTyped* pmsg,  sql::Statement* pConn);
void  Async_RenamePlayerNameRole(CMsgTyped* pmsg,  sql::Statement* pConn);
void  Async_RenamePlayerNameMas(CMsgTyped* pmsg,  sql::Statement* pConn);
void  Async_RenamePlayerNameFamily(CMsgTyped* pmsg,  sql::Statement* pConn);
void  Async_RenamePlayerNameMail(CMsgTyped* pmsg,  sql::Statement* pConn);


void  Async_RenameFamilyName(CMsgTyped* pmsg,  sql::Statement* pConn);

void  Async_ReIDManage(CMsgTyped* pmsg,  sql::Statement* pConn);
void  Async_ReIDRole(CMsgTyped* pmsg,  sql::Statement* pConn);

//////////////////////////////////

struct MapStringComp
{
	bool operator()(const std::string& _X, const std::string& _Y) const
	{
		return (stricmp(_X.c_str() , _Y.c_str()) < 0);
	}
};

struct NameExist
{
public:
    void Print()
    {
        string s;
        for (int i=0; i < m_index.size(); i++)
        {
            s += IntAsString( m_index[i] );
            if(i != m_index.size()-1) s += ",";
        }
        printf(s.c_str());
        printf("\n");
    }
    vector<int> m_index;
};

class Worker : public CriticalObject
{
public:
        Worker();
        ~Worker();
public:
        void DoMerge();

        void AddPlayerName( const char* name, int index);
        void AddFamilyName(const char* name ,int index);

public:
        void DoJionTable(const char* tableName, int from_db, int to_db);

private:
        map<string, NameExist*, MapStringComp> m_playerNames;
        map<string, NameExist*, MapStringComp> m_FamilyNames;
};

extern Worker*  _WORKER;
